// login的控制器
const $ = require('jquery')
let axios = require("../api/index")
import login from "@/login.ejs"
export default function (router) {
    router.route("/login", (req, res) => {
        res.render(login({}))
        $(".loginStyle").on('click', async function (e) {
            e.preventDefault()
            let username = $(".username").val()
            let pwd = $(".pwd").val()
            console.log(username, pwd);
            let { data } = await axios.post("/login/loginUser", { username, pwd })
            // 接收到返回值后根据返回值判断是否登录成功,成功就跳向指定位置
            if (data.statu == 1) {
                //接收并存储token密钥,使用拦截器再每次发送时携带
                localStorage.setItem("token", data.token)
                await axios.get("/admin/adminName", {
                    params: {
                        loginName: username
                    }
                })
                router.go("/admin/users")
            }
        })
    })
}